package com.chezhujie.www.controller;

import com.chezhujie.www.domain.Czjmonitor;
import com.chezhujie.www.service.CzjmonitorService;
import com.sys.core.page.Pagination;
import com.sys.user.base.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 *  出行助手  链接 监控
 * CzjmonitorController
 * 创建人:cheny
 * 时间：2017年6月9日-下午4:03:10 
 * @version 1.0.0
 *
 */
@Controller
@RequestMapping("/mon")
public class CzjmonitorController extends BaseController {
	private static final Logger log = LoggerFactory
			.getLogger(CzjmonitorController.class);

	public CzjmonitorController() {
	}

	/**
	 * 
	 * 方法名：save 拦截监控请求(czj_monitor)保存 创建人：RUI 时间：2017年5月24日-下午7:08:32
	 * 
	 * @param type
	 * @param request
	 * @param response
	 * @param model
	 * @throws Exception
	 *             void
	 * @exception
	 * @since 1.0.0
	 */
	@RequestMapping("czjmonitorSave")
	public void save(@RequestParam(required = true) Integer type,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) throws Exception {

		try {

			Czjmonitor czjmonitor = new Czjmonitor();
			czjmonitor.setType(type);
			czjmonitor.setCreateTime(new Date());
			czjmonitor.setTotals(1);
			czjmonitorService.svseCzjmonitor(czjmonitor);
			jsonResult(1, "", "成功", "", response);

		} catch (Exception e) {
			e.printStackTrace();
			log.info("error", e);
			jsonResult(0, "115", "接口监听失败", "", response);
		}
	}

	/**
	 * 
	 * 方法名：list 按照类型显示所有的访问量（可按照时间段查询） 创建人：RUI 时间：2017年5月25日-上午9:46:24
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws Exception
	 *             String
	 * @exception
	 * @since 1.0.0
	 */
	@RequestMapping("monitor_list")
	public String list(String beginTime, String endTime, Integer type,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) throws Exception {

		try {
			Map<String, Object> map = new HashMap<String, Object>();
			;
			if (StringUtils.isNotBlank(beginTime)) {
				map.put("beginTime", beginTime);
				map.put("createTime", 11);
				if (StringUtils.isBlank(endTime))
					map.put("endTime", new Date());
				else
					map.put("endTime", endTime);
			}

			if (!"".equals(type) && null != type) {
				map.put("type", type);
				model.addAttribute("type", type);
			} else {
				map.put("type", 0);
				model.addAttribute("type", 0);
			}

			List<Czjmonitor> czjmonitors = czjmonitorService
					.listCzjmonitor(map);
			model.addAttribute("czjmonitors", czjmonitors);
			if (StringUtils.isNotBlank(beginTime)) {
				model.addAttribute("beginTime", beginTime);
			}
			if (StringUtils.isNotBlank(endTime)) {
				model.addAttribute("endTime", endTime);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "mon/monitor_list";
	}
	
	@RequestMapping("monitor_datail")
	public String monitor_datail(String beginTime, String endTime, Integer type,
			HttpServletRequest request, HttpServletResponse response,
			Integer pageSize,Integer pageNo,
			ModelMap model) throws Exception {
		
		try {
			if(null==pageSize)
				pageSize=20;
			if(null==pageNo)
				pageNo=1;
			
			Map<String, Object> map = new HashMap<String, Object>();
			;
			if (StringUtils.isNotBlank(beginTime)) {
				map.put("beginTime", beginTime);
				map.put("createTime", 11);
				if (StringUtils.isBlank(endTime))
					map.put("endTime", new Date());
				else
					map.put("endTime", endTime);
			}

			if (!"".equals(type) && null != type) {
				map.put("type", type);
				model.addAttribute("type", type);
			} else {
				map.put("type", 0);
				model.addAttribute("type", 0);
			}

			Pagination pagination = czjmonitorService
					.listmon(map,pageSize,pageNo);
			model.addAttribute("pagination", pagination);
			if (StringUtils.isNotBlank(beginTime)) {
				model.addAttribute("beginTime", beginTime);
			}
			if (StringUtils.isNotBlank(endTime)) {
				model.addAttribute("endTime", endTime);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
		return "mon/monitor_detail";
	}

	@Resource
	private CzjmonitorService czjmonitorService;

}